package demo.event;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class DemoActionEventFrame extends JFrame{
	
	private JButton btnHello, btnClear;
	private JLabel lbHello;
	
	public DemoActionEventFrame(){
		//Thiet dat tieu de cho frame
		super("Demo ActionEvent");
		initComponent();
	}
	
	private void initComponent(){
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		//Tao button Hello
		btnHello = new JButton("Hello");
		//gan su kien ActionEvent cho button Hello
		btnHello.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent ev) {
				//Code xu ly su kien click tren button dat o day
				lbHello.setText("Hello there!");
			}
			
		});
		
		//Tao button Clear
		btnClear = new JButton("Clear text");
		//gan su kien ActionEvent cho button Clear
		btnClear.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent ev) {
				lbHello.setText("");
			}
					
		});
		
		//Tao ra panel de chua 2 button tren
		JPanel pnButton = new JPanel(new FlowLayout());
		
		//Dua 2 button tren vao panel
		pnButton.add(btnHello);
		pnButton.add(btnClear);
		
		c.add(pnButton, BorderLayout.NORTH);
		
		//Tao Label
		lbHello = new JLabel();
		c.add(lbHello, BorderLayout.CENTER);
	}
}